Key idea is dependency from one point in time/space to the next!
This implies a differential/difference equation to describe the process/transfer function
Dynamic models always involves derivatives (equations that express how things change from time step to time step or place to place )
Implement population growth as a derivative - a model of population change
# note that we include time here but we don't use it; we will need this later
source("../R/dexppop.R")
dexppop## function (time, P, r)
## {
## dexpop = r * P
## return(list(dexpop))
## }
## [[1]]
## [1] 0.2
## [[1]]
## [1] 0.2
# lets look at this for a range of initial populations
pops = seq(from=1, to=100)
tmp = pops %>% map(~dexppop( time=0,r=0.01, P=.x))
pchange = unlist(tmp)
pdyn = data.frame(pops, pchange)
ggplot(pdyn, aes(pops, pchange))+geom_point(col="green", size=1.5)What if we wanted to look at population in 20 years given an initial condition
Two options
explicit solution to differential equation is known; e.g. you can integrate both sides of the equation! Not always possible but lets look at a case where it is possible
must be solved by iteration; this is what we do when we can’t integrate both sides
## function (T, P0, r, K)
## {
## P = P0 * exp(r * T)
## if (P > K) {
## P = K
## }
## return(P)
## }
# gives population after any time given an initial population
# 20 rabbits, growth rate of 0.01 how many in 30 years
exppop(T=30, P0=20, r=0.01, K=1000)## [1] 26.99718
# if we want to see how population evolves over time - generate a time series by running our model for each point in time
initialrabbits = 20
years = seq(from=1, to=100, by=2)
Ptime = years %>% map_dbl(~exppop( P0=initialrabbits, r=0.01, K=1000, T=.x))
# keep track of what times we ran
Ptime = data.frame(P=Ptime, years=years)
ggplot(Ptime, aes(years,P))+geom_point()+labs(x="years",y="Rabbit Population")# try generating results for maximum and minimum possible r values to compare (guess at what you think)
max_r = 0.1
min_r = 0.01
K = 1000
tmp = years %>% map_dbl(~exppop(r=max_r, P0=initialrabbits, K=K, T=.x))
Ptime$Pmaxr = tmp
tmp = years %>% map_dbl(~exppop(r=min_r, P0=initialrabbits, K=K, T=.x))
Ptime$Pminr = tmp
head(Ptime)## P years Pmaxr Pminr
## 1 20.20100 1 22.10342 20.20100
## 2 20.60909 3 26.99718 20.60909
## 3 21.02542 5 32.97443 21.02542
## 4 21.45016 7 40.27505 21.45016
## 5 21.88349 9 49.19206 21.88349
## 6 22.32556 11 60.08332 22.32556
Ptimep = Ptime %>% gather(key="r",value="P",-years)
ggplot(Ptimep, aes(years,P, col=r))+geom_point()+labs(x="years",y="Rabbit Population")